<?php
namespace Siter\Controller;

class SiteController extends Controller {

    public function index() {
        $m    = D2();
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page);
		$this->display();
    }

    public function modify() {
        return false;

        $id = empty($_GET['id']) ? 0 : (int)$_GET['id'];

        if(empty($id)) {
            throw new ThinkException('参数不完整，无法获取数据');
        }

        $m = D2();

        if(!$record = $m->getByZoneId($id)) {
            throw new ThinkException('没有找到广告位信息');
        }

        $mAd = D2('Ad');
        $rAd = $mAd->getAdsByTypeAndSize($record['zone_type'], $record['zone_width'], $record['zone_height']);

        $this->assign('ad', $rAd);
        $this->assign('record', $record);
        $this->display();
    }

    public function doAdd() {
        $uid = C('RUN_LOGIN_USER.uid');

        if(empty($uid)) {
            showJson(array('s' => false, 'm' => '参数不完整'));
        }

        $dao = array(
            'site_name'  => I('post.site_name',   ''),
            'site_desc'  => I('post.site_desc',   ''),
            'domain_dao' => I('post.domain_dao', ''),
            'site_type'  => I('post.site_type',   ''),
            'uid'        => $uid,
        );
        $m = D2();

        try {
            $msg = $m->save2($dao) ? array('s' => true, 'm' => '保存成功') : array('s' => false, 'm' => '保存出错');
        } catch(Exception $e) {
            $msg = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($msg);
    }

    public function doSave() {
        return false;

        $d = $_POST;
        $m = D2();

        try {
            $r = $m->save2($d) ? array('s' => true, 'm' => '保存成功') : array('s' => false, 'm' => '保存出错');
        } catch (Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        echo json_encode($r);
    }

    /**
     * getCode
     * 获取广告位代码页面
     *
     * @return void
     */
    public function getCode() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            throw new ThinkException('参数不完整');
        }

        $m = D2();

        if(!$record = $m->getByZoneId($id)) {
            throw new ThinkException('没有找到广告位');
        }

        $this->assign('record', $record)->display();
    }

    public function del() {
        $id = empty($_GET['id']) ? 0 : (int)$_GET['id'];

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整，无法删除数据'));
        }

        $m = D2();
        $r = $m->del($id) ? array('s' => true, 'm' => '删除成功') : array('s' => false, 'm' => '删除出错');

        showJson($r);
    }

}
